package ar.com.photo_admin.service

import org.grails.plugins.imagetools.*
import ar.com.photo_admin.domain.Event;
import ar.com.photo_admin.domain.EventPhoto;

class EventPhotoService extends BaseService{
	
	def THUMBNAIL_SIZE = 100
	def THUMBNAIL_MEDIUM_SIZE = 200
	
    def deletePhoto(photoId) {
		def photo = EventPhoto.get(photoId);		
		photo.delete(flush:true);
    }
	
	def findEventPhotoById(def photoId){
		def photo = EventPhoto.get(photoId);
		photo = photo.refresh();
		return photo;
	}	
	
	def generateThumbnail(EventPhoto eventPhoto, thumbnailSize) {
		def imageTool = new ImageTool()						
		
		imageTool.load(eventPhoto.pathInServer)		
		imageTool.square()
		imageTool.swapSource()
		imageTool.thumbnail(thumbnailSize)
		imageTool.writeResult(eventPhoto.thumbnailPathInServer, "JPEG")			
	}
	
	def generateMediumThumbnail(EventPhoto eventPhoto, thumbnailSize) {
		def imageTool = new ImageTool()								
		imageTool.load(eventPhoto.pathInServer)						
		imageTool.thumbnail(thumbnailSize)
		imageTool.writeResult(eventPhoto.thumbnailMediumPathInServer, "JPEG")			
	}
	
	/**
	 * Crea la foto temporalmente. Todavia no la asigna al evento.
	 * @param eventId
	 * @param fileBytes
	 * @param originalFilename
	 * @return
	 */
	def createPhotoTemporally(def eventNumber, byte[] fileBytes ,def originalFilename){

		def filename = originalFilename;
		def filenameWithoutExtension = splitStringInNameAndExtension(filename)[0]
								
		
		//Armo la url
		def eventUrl = SERVER_URL+ eventNumber + "/"
		def serverUrl = eventUrl + filename
		def serverThumbnailUrl = eventUrl + filenameWithoutExtension +"_s.JPEG";
		def serverThumbnailMediumUrl = eventUrl + filenameWithoutExtension +"_m.JPEG";
		
		
		//Armo el path donde se almacenara en el servidor
		def eventPath = getImagesRootPath() + eventNumber +File.separator
		def photoPath = eventPath +filename
		def thumbnailPath = eventPath+ filenameWithoutExtension +"_s.JPEG";
		def thumbnailMediumPath = eventPath+ filenameWithoutExtension +"_m.JPEG";
		

		def destinationFile = new File(photoPath);

		
		destinationFile.delete()
		destinationFile << fileBytes
		
		def photo = new EventPhoto(url:serverUrl, pathInServer:photoPath,
			thumbnailPathInServer:thumbnailPath, thumbnailMediumPathInServer:thumbnailMediumPath, 
			thumbnailUrl:serverThumbnailUrl, thumbnailMediumUrl:serverThumbnailMediumUrl,
			name: filename, temporalEventNumber: eventNumber);
		
		this.generateThumbnail(photo, THUMBNAIL_SIZE)
		this.generateMediumThumbnail(photo, THUMBNAIL_MEDIUM_SIZE)

		if(photo.hasErrors()){
			log.error "Error en la foto almacenada: " + photo.errors;
		}
	
		photo = photo.save(flush:true);
		
		return photo;
	}
	
	
	def deletePhotoTemporally(photoId){		
		EventPhoto photo = this.findEventPhotoById(photoId);
		photo.toBeDeleted = true
		photo.save(flush:true);	
	}


	
	def splitStringInNameAndExtension(name){
		def vars = name.split("\\.");
		
		def start = new StringBuilder();
		for(int i=0;i<vars.size()-1;i++){
			start.append(vars[i]);
		}
		
		return [ start , vars[vars.size()-1] ]
	}
		
}
